package com.zx.erp.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.erp.sys.bean.Module;
import com.zx.erp.sys.bean.Permission;
import com.zx.erp.sys.mapper.PermissionMapper;
import com.zx.erp.sys.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者信息
 * @since 2021-11-10
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private PermissionMapper pdao;

    @Override
    public List<Module> findMoudleByJob(Integer jobId, Integer pstate, Integer flag) {
        List<Module> modules = pdao.findMoudleByJob(jobId, pstate, flag);
        return modules;
    }

    @Override
    public Boolean grantPermission(Integer jobId, List<Permission> plist) {
        boolean flag = false;
        //先根据职位id获取 其原有的主键id
        List<Permission> permissions = pdao.selectByJobId(jobId);
        try {
            for (Permission p : permissions) {
                flag = pdao.deleteById(p.getComid()) > 0 ? true : false;
            }
            for (Permission p : plist) {
                flag = pdao.insert(p) > 0 ? true : false;
            }
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }
}
